package org.dreamwork.websocket;

import org.apache.log4j.Logger;
import org.dreamwork.util.StringUtil;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import java.util.Map;

/**
 * Created by game on 2017/3/4
 */
public class WebSocketHttpConfigurator extends ServerEndpointConfig.Configurator {
    private static final Logger logger = Logger.getLogger (WebSocketHttpConfigurator.class);

    public static final String KEY_HTTP_SESSION = "org.dreamwork.websocket.conf.KEY_HTTP_SESSION";
    public static final String KEY_HTTP_HEADER  = "org.dreamwork.websocket.conf.KEY_HTTP_HEADER";
    public static final String KEY_HTTP_CONTEXT = "org.dreamwork.websocket.conf.KEY_HTTP_CONTEXT";
    public static final String KEY_HTTP_PARAM   = "org.dreamwork.websocket.conf.KEY_HTTP_PARAM";
    public static final String KEY_HTTP_QUERY   = "org.dreamwork.websocket.conf.KEY_HTTP_QUERY_STRING";
    public static final String KEY_HTTP_URI     = "org.dreamwork.websocket.conf.KEY_HTTP_URI";

    boolean httpSessionSupported, httpHeaderSupported, servletContextSupported, httpParameterSupported;

    WebSocketHttpConfigurator () {}

    WebSocketHttpConfigurator (boolean httpSessionSupported, boolean httpHeaderSupported, boolean servletContextSupported, boolean httpParameterSupported) {
        this.httpSessionSupported = httpSessionSupported;
        this.httpHeaderSupported = httpHeaderSupported;
        this.servletContextSupported = servletContextSupported;
        this.httpParameterSupported = httpParameterSupported;
    }

    @Override
    public void modifyHandshake (ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        HttpSession session = (HttpSession) request.getHttpSession ();
        Map<String, Object> map = sec.getUserProperties ();

        if (httpHeaderSupported) {
            map.put (KEY_HTTP_HEADER, request.getHeaders ());
            if (logger.isDebugEnabled ()) {
                logger.debug ("put HTTP HEADER as " + KEY_HTTP_HEADER);
                logger.debug ("    the headers = " + request.getHeaders ());
            }
        }

        if (httpParameterSupported) {
            map.put (KEY_HTTP_PARAM, request.getParameterMap ());
            if (logger.isDebugEnabled ()) {
                logger.debug ("put HTTP PARAMETERS as " + KEY_HTTP_PARAM);
                logger.debug ("    the parameters = " + request.getParameterMap ());
            }
        }

        if (httpSessionSupported && session != null) {
            map.put (KEY_HTTP_SESSION, session);
            if (logger.isDebugEnabled ()) {
                logger.debug ("put HTTP SESSION as " + KEY_HTTP_SESSION);
                logger.debug ("    the httpSession = " + session);
            }
        }

        if (servletContextSupported && session != null) {
            map.put (KEY_HTTP_CONTEXT, session.getServletContext ());
            if (logger.isDebugEnabled ()) {
                logger.debug ("put Servlet Context as " + session.getServletContext ());
            }
        }

        String queryString = request.getQueryString ();
        if (!StringUtil.isEmpty (queryString)) {
            map.put (KEY_HTTP_QUERY, queryString);
        }
        map.put (KEY_HTTP_URI, request.getRequestURI ());

        if (logger.isDebugEnabled ()) {
            logger.debug ("put HTTP QUERY STRING as " + KEY_HTTP_QUERY);
            logger.debug ("    the query string = " + request.getQueryString ());
            logger.debug ("put HTTP URI as " + KEY_HTTP_URI);
            logger.debug ("    the uri = " + request.getRequestURI ());
        }
    }
}